/*
 * @Author: weidewei
 * @Date: 2025-11-03 14:29:16
 * @LastEditors: weidewei
 * @LastEditTime: 2025-11-03 17:13:18
 * @Description:
 * @FilePath: \2025-front-face\js常见手写题\throttle.js
 */
// 节流​​：在​​固定时间间隔内​​，无论事件触发多少次，​​只执行一次函数​​。
function throttleTimestamp(fn, wait = 500) {
  let lastTime = 0;
  return function (...args) {
    let nowTime = Date.now();
    // 1. 判断是否达到执行间隔
    if (nowTime - lastTime >= wait) {
      // 2. 执行函数
      fn.apply(this, args);
      // 3. 更新最后执行时间
      lastTime = nowTime;
    }
  };
}

function throttleSettimeout(fn, wait = 500) {
    let timeout = 0;
    return function (...args) {
        if(timeout) return;
        timeout = setTimeout(()=>{
            fn.apply(this,args);
            timeout = null;
        },wait);
    }
}
